﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading.Tasks;
using System.ComponentModel;
using XchangeStreamer.OutputSdk;

namespace WpfClient
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Connector connector;
        public MainWindow()
        {
            InitializeComponent();
            this.Closing += new System.ComponentModel.CancelEventHandler(MainWindow_Closing);

            Task.Factory.StartNew(this.Connect);
        }

        void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            this.connector.Disconnect();
            this.connector.Dispose();
        }

        private void Connect()
        {
            this.connector = Connector.Connection;
            this.connector.Connect();

            this.Dispatcher.Invoke(new Action(this.Bind));
        }

        private void Bind()
        {
            //this.market.ItemsSource = this.connector.Market;

            this.bids.ItemsSource = this.connector.Market.Find("EGS74081C018").MarketDepthByPrice_Bids;
            this.asks.ItemsSource = this.connector.Market.Find("EGS74081C018").MarketDepthByPrice_Asks;
            this.trades.ItemsSource = this.connector.Market.Find("EGS74081C018").Trades;

            this.trades.Columns[0].SortDirection = ListSortDirection.Descending;
            this.trades.Items.SortDescriptions.Add(new SortDescription("Time", ListSortDirection.Descending));

            this.bids.Columns[0].SortDirection = ListSortDirection.Descending;
            this.bids.Items.SortDescriptions.Add(new SortDescription("Price", ListSortDirection.Descending));

            this.asks.Columns[0].SortDirection = ListSortDirection.Ascending;
            this.asks.Items.SortDescriptions.Add(new SortDescription("Price", ListSortDirection.Ascending));

            //this.connector.Market.Find("EGS74081C018").MarketDepthByPrice_Bids.RemoveAt(0);
            //this.connector.Market.Find("EGS74081C018").MarketDepthByPrice_Asks.RemoveAt(0);
            //this.connector.Market.Find("EGS74081C018").Trades.RemoveAt(0);
        }
    }
}